<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList.value" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script setup>
import { reactive, ref } from "vue";
import {onLoad,onReachBottom,onPullDownRefresh} from '@dcloudio/uni-app'

  const queryObj=reactive({
    query:'',
    cid:'',
    pagenum:1,
    pagesize:10
  })
  const goodsList=reactive(
    {
      value:[]
    }
  )
  const total=ref(0)
  const isloading=ref(false)

  
  onLoad((params)=>{
    queryObj.query=params.query || ""
    queryObj.cid=params.cid || ""
    getGoodsList()
  }  
  )
  
  onReachBottom(()=>{
    if(queryObj.pagenum*queryObj.pagesize>=total.value) return uni.$showMsg('数据加载完毕！')
    if(isloading.value) return
    queryObj.pagenum+=1
    getGoodsList()
  })
  
  onPullDownRefresh(()=>{
    queryObj.pagenum=1
    total.value=0
    isloading.value=false
    goodsList.value=[]
    getGoodsList(()=>uni.stopPullDownRefresh())
  })
  
  const getGoodsList=async(cb)=>{
          isloading.value=true
          const { data: res } = await uni.$http.get('/api/public/v1/goods/search', queryObj)
          isloading.value=false
          cb&&cb()
          if (res.meta.status !== 200) return uni.$showMsg()
          goodsList.value=goodsList.value.concat(res.message.goods)
          total.value = res.message.total
  }
  
  const gotoDetail=(item)=>{
     uni.navigateTo({
        url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
      })
  }

</script>

<style lang="scss">

</style>
